package action;

import org.apache.struts2.interceptor.validation.SkipValidation;

import service.UsersDAO;
import service.impl.UsersDAOImpl;

import com.opensymphony.xwork2.ModelDriven;

import entity.Users;

public class UsersAction extends SuperAction implements ModelDriven<Users>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Users user = new Users();
	
	public String login(){
		
		UsersDAO usersDAO = new UsersDAOImpl();
		if (usersDAO.usersLogin(user)) {
			httpSession.setAttribute("loginUserName", user.getUsername());
			
			return "login_success";
		}else{
			return "login_failure";
		}
		
	}
	
	@SkipValidation
	public String logout(){
		
		if(httpSession.getAttribute("loginUserName")!=null){
			httpSession.removeAttribute("loginUserName");
		}
		return "logout_success";
		
	}
	
	
	
	
	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#validate()
	 */
	@Override
	public void validate() {
		if ("".equals(user.getUsername().trim())) {
			this.addFieldError("usernameError", "用户名不能为空!");
		}
		
		if (user.getPassword().length()<6) {
			this.addFieldError("passwordError", "密码不能少于6位!");
		}
	}

	public Users getModel(){
		return this.user;
	}
}
